<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>
        人元添加
    </title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="telephone=no" name="format-detection">
    <link href="css/main.css" media="all" rel="stylesheet">
    <style>
        .imgs {
            display: none;
        }

        input[type="file"] {
            width: 50px;
            height: 50px;
            border-radius: 100%;
            opacity: 0;
            cursor: pointer;
            border: 1px solid red;
            position: absolute;
        }
    </style>

</head>
<body>

<div class="x-body">
    <form action="" class="layui-form layui-form-pane" id="add">
        <blockquote class="layui-elem-quote">
            <input name="id" type="hidden" value="8">
            <div>
                <input accept="undefined" class="layui-upload " id="previewImg" name="images" onchange="upload(this,8)"
                       type="file">
                <img alt="个人头像" class="layui-circle" id="pimages" name="pimages"
                     src="./images/tx.jpg" style="width:50px;height:50px;float:left">
                <input id="avatar" name="image" required="" type="hidden" value="./images/tx.jpg">
                <dl style="margin-left:80px; color:#019688">
                    <dt><span>ADMIN</span> <span></span></dt>
                    <dd style="margin-left:0">这家伙很懒，什么也没有留下</dd>
                </dl>
            </div>
        </blockquote>

        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">


            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">


                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>用户名
                        </label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" name="name"
                                   placeholder="空制在80个汉字，160个字符以内"
                                   readonly type="text" value="ADMIN">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>电话号码
                        </label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" name="phone" placeholder=""
                                   type="text" value="15325644722">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>新密码
                        </label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" name="password" placeholder="" type="password"
                                   value="">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class='x-red'>*</span>重复新密码
                        </label>
                        <div class="layui-input-block">
                            <input autocomplete="off" class="layui-input" name="newpassword"
                                   placeholder="新密码为空则不修改"
                                   type="password"
                                   value="">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <button class="layui-btn" lay-filter="add" lay-submit="" type="button">
                            保存
                        </button>
                    </div>
                    <!--</form>-->
                    <div style="height:100px;"></div>
                </div>


            </div>

        </div>
    </form>
</div>
<script charset="utf-8" src="lib/layui/layui.js"></script>
<script charset="utf-8" src="js/x-layui.js"></script>
<script src="/js/jquery-3.7.1.min.js"></script>
<script>
    layui.use(['element', 'layer', 'form'], function () {
        $ = layui.jquery;//jquery
        lement = layui.element();//面包导航
        layer = layui.layer;//弹出层
        form = layui.form()

        // 从sessionStorage获取用户数据
        function loadUserData() {
            // 从session获取当前用户ID
            const userId = sessionStorage.getItem("userId");
            if (!userId) {
                layer.msg('用户未登录', {icon: 5});
                return;
            }

            // 设置隐藏的ID字段
            $("input[name='id']").val(userId);

            // 模拟从session获取数据
            const userData = {
                name: sessionStorage.getItem("username") || "ADMIN",
                phone: sessionStorage.getItem("phone") || "15325644722",
                avatar: sessionStorage.getItem("avatar") || "./images/tx.jpg"
            };

            // 填充表单数据
            $("input[name='name']").val(userData.name);
            $("input[name='phone']").val(userData.phone);
            $("#pimages").attr('src', userData.avatar);
            $("#avatar").val(userData.avatar);
            $("dt span:first").text(userData.name);
        }

        function validatePassword() {
            const password = $("input[name='password']").val();
            const newPassword = $("input[name='newpassword']").val();

            if (!password) {
                layer.msg('密码不能为空', {icon: 5});
                return false;
            }

            if (newPassword && password !== newPassword) {
                layer.msg('两次输入的密码不一致', {icon: 5});
                return false;
            }
            return true;
        }


        // 监听提交
        form.on('submit(add)', function (data) {
            var formData = data.field;
            console.log(formData)
            if (!validatePassword()) {
                return false;
            }
            // 添加当前用户ID
            formData.userId = sessionStorage.getItem("userId");
            console.log(formData)
            $.ajax({
                type: "post",
                url: "/user/updateInfo", // 替换为你的实际API地址
                data: {
                    id: formData.userId,
                    phone: formData.phone,
                    password: formData.password,
                },
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        // 更新session中的数据
                        // sessionStorage.setItem("userName", formData.name);
                        // sessionStorage.setItem("phone", formData.phone);

                        layer.msg('保存成功', {icon: 6}, function () {
                            window.parent.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                },
                error: function () {
                    layer.msg('保存失败', {icon: 5});
                }
            });
        });

        // 页面加载完成后获取数据
        $(function () {
            loadUserData();
        });
    })
</script>


<!--栏目缩略图上传-->
<script>
    function upload(obj, id) {
        var formData = new FormData();
        formData.append('images', $('#previewImg')[0].files[0]);
        formData.append('id', id);
        layer.msg('图片上传中', {icon: 16});
        $.ajax({
            type: "post",
            processData: false,
            contentType: false,
            url: "xxx",
            data: formData,
            success: function (data) {
                if (data.status == 1) {
                    console.log(data.image_name);
                    layer.closeAll('loading');
                    //layer.msg(data.info,{icon:1,time:1000});
                    $("#pimages").attr('src', data.image_name);
                    $("#avatar").val(data.image_name);
                    $(".imgs").show();
                    return false;
                } else {
                    layer.msg(data.info, {icon: 2, time: 1000});
                }
            }
        });
    }
</script>

</body>
</html>